# 获取高拍仪数据
import queue
import pytesseract
import os, time
from PIL import Image

import gevent
from gevent import monkey
from gevent.pool import Pool
monkey.patch_all()      # 携程插件

class get_H_str():
    def __init__(self, sinOUT=None):
        self.Hq = queue.Queue()
        self.img_path = r"E:\OCR_RMC\ORC_BAR2.0\OCR_RMC\出张出票"
        self.p = Pool(2)   # 控制并发数量  携程池
        self.sinOUT = sinOUT  # 信息展示

    def start_run(self, img_path):
        im = Image.open(img_path)
        get_data = pytesseract.image_to_string(im, lang='chi_sim')
        a = get_data.replace('\n', ',').split(',')
        b = a[2:]
        self.sinOUT.emit("H&%s" % str(b))

    def start(self):
        start_time = time.time()
        self.sinOUT.emit("H&高拍仪开始运行")

        start_list = []
        img_name_list = os.listdir(self.img_path)
        # print("img_name_list: ", img_name_list)
        while img_name_list:
            path = img_name_list.pop()
            img_path_list = os.path.join(self.img_path, path)
            start_list.append(self.p.spawn(self.start_run, img_path_list))  # 协程池启动列表
        # 启动协程
        gevent.joinall(start_list)
        H_sin = "高拍仪用时: %s" % str(time.time() - start_time)
        self.sinOUT.emit("H&%s" % H_sin)

        # 临时数据配置
        import configparser
        OCR_read = configparser.ConfigParser()
        OCR_read.read(r'E:\OCR_RMC\ORC_BAR2.0\OCR_RMC\H_OCR.ini', encoding='utf8')

        H_bar = OCR_read.get('H_bar', 'bar')
        H_bar2 = OCR_read.get('H_bar', 'bar2')
        H_data = eval(H_bar)
        H_data2 = eval(H_bar2)
        self.Hq.put(H_data)
        self.Hq.put(H_data2)
        self.sinOUT.emit("H&高拍仪结束运行")






